package com.zxt.content.copywriting.service.impl;

import com.zxt.content.common.utils.ZXTBeanUtils;
import com.zxt.content.common.utils.ZXTCollectionsUtil;
import com.zxt.content.copywriting.manager.UserCopywritingConfigManager;
import com.zxt.content.copywriting.resp.UserCopywritingConfigVO;
import com.zxt.content.copywriting.service.IUserCopywritingConfigService;
import com.zxt.content.user.manager.BUserManager;
import com.zxt.content.user.model.BUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zxt
 */
@Service(value = "userCopywritingConfigServiceImpl")
@AllArgsConstructor
public class UserCopywritingConfigServiceImpl implements IUserCopywritingConfigService {

    private final UserCopywritingConfigManager userCopywritingConfigManager;
    private final BUserManager bUserManager;

    @Override
    public List<UserCopywritingConfigVO> queryList() {
        return ZXTBeanUtils.beanCopy(userCopywritingConfigManager.list(), UserCopywritingConfigVO.class);
    }

    @Override
    public List<UserCopywritingConfigVO> getEnablePushUser() {
        List<UserCopywritingConfigVO> vos = ZXTBeanUtils.beanCopy(userCopywritingConfigManager.getEnablePushUser(), UserCopywritingConfigVO.class);

        if (ZXTCollectionsUtil.isEmpty(vos)) {
            return new ArrayList<>();
        }

        List<String> userIds = vos.stream().map(UserCopywritingConfigVO::getUserId).collect(Collectors.toList());

        List<BUser> users = bUserManager.listByIds(userIds);
        Map<Long, BUser> userMap = Optional.of(users).orElse(new ArrayList<>()).stream().collect(Collectors.toMap(BUser::getId, Function.identity()));

        for (UserCopywritingConfigVO vo : vos) {
            BUser bUser = userMap.get(vo.getUserId());
            if (Objects.nonNull(bUser)) {
                vo.setEmail(bUser.getEmail());
            }
        }
        return vos;
    }
}
